package com.spx.chapter06;

import com.spx.chapter05.pojo.Event;
import com.spx.chapter06.entity.UrlViewCount;
import com.spx.util.SampleDataUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * create by undeRdoG on  2022-05-02  21:51
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class UrlCountExample {

    public static void main(String[] args) throws Exception {

        /*
        *  统计 url的访问次数
        * */
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<Event> dataSource = env.fromCollection(SampleDataUtil.getSample())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Event>forBoundedOutOfOrderness(Duration.ofMillis(1L))
                                .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                                    @Override
                                    public long extractTimestamp(Event element, long recordTimestamp) {
                                        return element.timestamp;
                                    }
                                })
                );

        WindowedStream<Event, String, TimeWindow> window = dataSource.keyBy(data -> data.url)
                .window(TumblingEventTimeWindows.of(Time.seconds(10L)));


        /*
        *  关于AggregateFunction 和 ProcessWindowFunction的结合使用
        *  这样调用的处理机制是：基于第一个参数（增量聚合函数）来处理窗口数据，每来一个数
        *  据就做一次聚合；等到窗口需要触发计算时，则调用第二个参数（全窗口函数）的处理逻辑输
        *  出结果。需要注意的是，这里的全窗口函数就不再缓存所有数据了，而是直接将增量聚合函数
        * 的结果拿来当作了 Iterable 类型的输入。一般情况下，这时的可迭代集合中就只有一个元素了。
        *
        * */

        SingleOutputStreamOperator<UrlViewCount> res = window.aggregate(new AggURLFunction(), new UrlViewCountFunction());

        res.print("result");

        env.execute();
    }


    public static class AggURLFunction implements AggregateFunction<Event,Long,Long>{

        @Override
        public Long createAccumulator() {
            return 0L;
        }

        @Override
        public Long add(Event value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return null;
        }
    }


    public static class UrlViewCountFunction extends ProcessWindowFunction<Long, UrlViewCount,String,TimeWindow>{

        @Override
        public void process(String key, Context context, Iterable<Long> elements, Collector<UrlViewCount> out) throws Exception {
            out.collect(new UrlViewCount(key,elements.iterator().next(),context.window().getStart(),context.window().getEnd()));
        }
    }
}
